home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-14 | 7.7 KB | 247 lines | [TEXT/CWIE] |
- // ===========================================================================
- // File: LColorSwatch.cp
- // Version: 1.0 - Feb 08, 1995
- // Author: Mike Shields (mshields@inconnect.com)
- //
- // Copyright ©1996 Mike Shields. All rights reserved.
- // I hereby grant users of LColorSwatch permission to use it (or any modified
- // version of it) in applications (or any other type of Macintosh software
- // like extensions -- freeware, shareware, commercial, or other) for free,
- // subject to the terms that:
- //
- // (1) This agreement is non-exclusive.
- //
- // (2) I, Mike Shields, retain the copyright to the original source code.
- //
- // These two items are the only required conditions for use. However, I do have
- // an additional request. Note, however, that this is only a request, and
- // that it is not a required condition for use of this code.
- //
- // (1) That I be given credit for LColorSwatch code in the copyrights or
- // acknowledgements section of your manual or other appropriate documentation.
- //
- //
- // I would like to repeat that this last item is only a request. You are prefectly
- // free to choose not to do any or all of them.
- //
- // This source code is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- // ===========================================================================
- // LColorSwatch.h <- double-click + Command-D to see class declaration
- //
- // Originally based on the code by Brian Hill <lorax@msn.fullfeed.com>
- #include "LColorSwatch.h"
- #include <ColorPicker.h>
-
- // ---------------------------------------------------------------------------
- // • CreateFromStream
- // ---------------------------------------------------------------------------
- // Static function to create a ColorSwatch from the data in a stream
- LColorSwatch* LColorSwatch::CreateFromStream(LStream *inStream)
- {
- return(new LColorSwatch(inStream));
- }
-
- // ---------------------------------------------------------------------------
- // • LColorSwatch
- // ---------------------------------------------------------------------------
- // Default constructor
- LColorSwatch::LColorSwatch()
- {
- mColor.red = 0x0000;
- mColor.green = 0x0000;
- mColor.blue = 0x0000;
- }
-
- // ---------------------------------------------------------------------------
- // • LColorSwatch
- // ---------------------------------------------------------------------------
- // Copy constructor
- LColorSwatch::LColorSwatch(const LColorSwatch& inOriginal)
- : LControl(inOriginal)
- {
- mColor.red = inOriginal.mColor.red;
- mColor.green = inOriginal.mColor.green;
- mColor.blue = inOriginal.mColor.blue;
-
- mPrompt = inOriginal.mPrompt;
- }
-
- // ---------------------------------------------------------------------------
- // • LColorSwatch
- // ---------------------------------------------------------------------------
- // create a color swatch from parameters
- LColorSwatch::LColorSwatch(const SPaneInfo &inPaneInfo, MessageT inValueMessage,
- Int32 inValue, Int32 inMinValue, Int32 inMaxValue,
- ConstStringPtr inString, RGBColor& inColor)
- : LControl(inPaneInfo, inValueMessage, inValue, inMinValue, inMaxValue)
- {
- mColor.red = inColor.red;
- mColor.green = inColor.green;
- mColor.blue = inColor.blue;
-
- mPrompt = inString;
- }
-
- // ---------------------------------------------------------------------------
- // • LColorSwatch
- // ---------------------------------------------------------------------------
- // create a ColorSwatch from a stream
- LColorSwatch::LColorSwatch(LStream* inStream)
- : LControl(inStream)
- {
- inStream->ReadData(&mColor, sizeof(RGBColor));
- inStream->ReadPString(mPrompt);
- }
-
- // ---------------------------------------------------------------------------
- // • ~LColorSwatch
- // ---------------------------------------------------------------------------
- // Destructor
- LColorSwatch::~LColorSwatch()
- {
- }
-
- // ---------------------------------------------------------------------------
- // • GetDescriptor
- // ---------------------------------------------------------------------------
- // Return prompt of a ColorSwatch as a string
- StringPtr LColorSwatch::GetDescriptor(Str255 outDescriptor) const
- {
- return LString::CopyPStr(mPrompt, outDescriptor);
- }
-
-
- // ---------------------------------------------------------------------------
- // • SetDescriptor
- // ---------------------------------------------------------------------------
- // Set prompt of a ColorSwatch from a string
- void LColorSwatch::SetDescriptor(ConstStringPtr inDescriptor)
- {
- mPrompt = inDescriptor;
- }
-
-
- // ---------------------------------------------------------------------------
- // • GetColor
- // ---------------------------------------------------------------------------
- // get the color currently stored in a color swatch
- void LColorSwatch::GetColor(RGBColor& outColor)
- {
- outColor.red = mColor.red;
- outColor.green = mColor.green;
- outColor.blue = mColor.blue;
- }
-
-
- // ---------------------------------------------------------------------------
- // • SetColor
- // ---------------------------------------------------------------------------
- // set the color currently stored in a color swatch
- void LColorSwatch::SetColor(RGBColor& inColor)
- {
- mColor.red = inColor.red;
- mColor.green = inColor.green;
- mColor.blue = inColor.blue;
- }
-
- // ---------------------------------------------------------------------------
- // • HotSpotAction
- // ---------------------------------------------------------------------------
- // Take action during mouse down tracking
- //
- // ColorSwatches toggle between two states depending on whether the cursor is
- // inside the border..
- void LColorSwatch::HotSpotAction(Int16 /*inHotSpot*/, Boolean inCurrInside,
- Boolean inPrevInside)
- {
- if ( inCurrInside != inPrevInside )
- {
- FocusDraw();
- StColorPenState thePenState;
- Rect frame;
-
- StColorPenState::Normalize();
-
- CalcLocalFrameRect(frame);
-
- if ( inCurrInside == true )
- {
- ::PenSize(2, 2);
- }
- ::EraseRect(&frame);
- ::FrameRect(&frame);
- ::InsetRect(&frame, 3, 3);
- ::RGBForeColor(&mColor);
- ::PaintRect(&frame);
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • HotSpotResult
- // ---------------------------------------------------------------------------
- // Display the color picker dialog and broadcast the fact the data's changed
- // if the user selected an new color.
- void LColorSwatch::HotSpotResult(Int16 inHotSpot)
- {
- Point where = {-1, -1};
- RGBColor outColor;
- Boolean colorChosen = ::GetColor(where, mPrompt, &mColor, &outColor);
-
- if ( colorChosen )
- {
- mColor.red = outColor.red;
- mColor.green = outColor.green;
- mColor.blue = outColor.blue;
- }
-
- // Undo Button hilighting
- HotSpotAction(inHotSpot, false, true);
-
- if ( colorChosen && (mValueMessage != cmd_Nothing) )
- {
- BroadcastMessage(mValueMessage, (void*)&outColor);
- }
- }
-
-
- // ---------------------------------------------------------------------------
- // • PointInHotSpot
- // ---------------------------------------------------------------------------
- // Point is in the frame if it is inside the color swatch in the control.
- Boolean LColorSwatch::PointInHotSpot(Point inPoint, Int16 /*inHotSpot*/)
- {
- Rect frame;
- Boolean isInFrame = false;
-
- if ( CalcLocalFrameRect(frame) )
- {
- ::InsetRect(&frame, 3, 3);
-
- isInFrame = ::PtInRect(inPoint, &frame);
- }
-
- return isInFrame;
- }
-
- // ---------------------------------------------------------------------------
- // • DrawSelf
- // ---------------------------------------------------------------------------
- void LColorSwatch::DrawSelf()
- {
- StColorPenState thePenState;
- Rect frame;
-
- StColorPenState::Normalize();
-
- CalcLocalFrameRect(frame);
-
- ::EraseRect(&frame);
- ::FrameRect(&frame);
- ::InsetRect(&frame, 3, 3);
- ::RGBForeColor(&mColor);
- ::PaintRect(&frame);
- }
-